#include "receipt_additem.h"
#include "ui_receipt_additem.h"

Receipt_AddItem::Receipt_AddItem(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Receipt_AddItem)
{
    ui->setupUi(this);

    table=TD_RECEIPT;
    ui->vouch_date->setDate(QDate::currentDate());
}

Receipt_AddItem::~Receipt_AddItem()
{
    delete ui;
}

bool Receipt_AddItem::addItem(const QString &vouchNo, const QString &vouchDate,
                              double vouchSum, const QString &vouchSource)
{
    QSqlQuery query;
    query.prepare(QString("insert into %1 (month_,date_,vouch_no,vouch_date,vouch_sum,vouch_source) values (?,?,?,?,?,?)")
                  .arg(table));
    query.addBindValue(CurrentMonth);
    query.addBindValue(Today);
    query.addBindValue(vouchNo);
    query.addBindValue(vouchDate);
    query.addBindValue(vouchSum);
    query.addBindValue(vouchSource);
    if(query.exec())
        return true;
    else
    {
        qDebug() << query.lastError().text();
        return false;
    }
}

void Receipt_AddItem::on_btn_submit_clicked()
{
    QString vouchNo=ui->vouch_no->text().trimmed();
    QString vouchDate=ui->vouch_date->date().toString(DATE_FORMAT);
    double vouchSum=ui->vouch_sum->value();
    QString vouchSource=ui->vouch_source->text().trimmed();

    if(addItem(vouchNo,vouchDate,vouchSum,vouchSource))
        accept();
    else {
        MESSAGE_CANNOT_SUBMIT
    }
}

void Receipt_AddItem::on_btn_paste_clicked()
{
    QString vouchNo=qApp->clipboard()->text().trimmed();
    if(!vouchNo.isEmpty())
        ui->vouch_no->setText(vouchNo);
}
